package ar.edu.itba.cg.menu;

import java.util.concurrent.Callable;

import org.fenggui.Container;
import org.fenggui.Display;
import org.fenggui.GameMenuButton;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.RowLayout;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Spacing;

import com.jme.image.Texture;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;

public class BowlingMenu {

	private GameMenuButton playButton, playersButton, quitButton, continueButton, endButton;
	private GameMenuButton oneButton, twoButton, threeButton, fourButton;
	private Display display;
	private FengJMEInputHandler handler;
	private final Container menu = new Container(new RowLayout(true));
	TextureState defaultTextureState = null;
	private boolean finish;
	private boolean change;
	private int players=1;

	public BowlingMenu() {
		setupMenuGUI();
	}

	private void setupMenuGUI() {
		display = new Display(
				new org.fenggui.binding.render.lwjgl.LWJGLBinding());

		DisplaySystem.getDisplaySystem().getRenderer().setBackgroundColor(
				ColorRGBA.darkGray);
		handler = new FengJMEInputHandler(display);
		menu.getAppearance().setPadding(new Spacing(10, 10));
		menu.setLayoutManager(new RowLayout(false));
		display.addWidget(menu);
		setupButtons(menu, display);
		buildMainMenu();
		setListeners(menu, display);
		display.layout();
	}

	private void setupButtons(Container c, Display d) {
		String path = "resources/";
		/* Main menu */
		playButton = new GameMenuButton(path + "jugar.png", path + "jugar.png");
		playersButton = new GameMenuButton(path + "jugadores.png", path
				+ "jugadores.png");
		quitButton = new GameMenuButton(path + "salir.png", path + "salir.png");
		
		/* Pause menu */
		continueButton= new GameMenuButton(path + "continuar.png", path + "continuar.png");
		endButton= new GameMenuButton(path + "finalizar.png", path + "finalizar.png");

		/* Players menu */
		oneButton = new GameMenuButton(path + "uno.png", path + "uno.png");
		twoButton = new GameMenuButton(path + "dos.png", path + "dos.png");
		threeButton = new GameMenuButton(path + "tres.png", path + "tres.png");
		fourButton = new GameMenuButton(path + "cuatro.png", path
				+ "cuatro.png");

	}

	private void setListeners(final Container c, final Display d) {

		playButton.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				finish = true;
				setActive(false);
			}
		});
		
		continueButton.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				finish = true;
				change=false;
				setActive(false);
			}
		});

		endButton.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				change=true;
				buildMainMenu();
			}
		});
		
		playersButton.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent e) {
				buildPlayersMenu();
			}

		});

		quitButton.addButtonPressedListener(new IButtonPressedListener() {

			public void buttonPressed(ButtonPressedEvent arg0) {
				System.exit(0);
			}
		});

		oneButton.addButtonPressedListener(new IButtonPressedListener() {
			public void buttonPressed(ButtonPressedEvent arg0) {
				players=1;
				buildMainMenu();
			}
		});

		twoButton.addButtonPressedListener(new IButtonPressedListener() {

			public void buttonPressed(ButtonPressedEvent arg0) {
				players=2;
				buildMainMenu();
			}
		});

		threeButton.addButtonPressedListener(new IButtonPressedListener() {

			public void buttonPressed(ButtonPressedEvent arg0) {
				players=3;
				buildMainMenu();
			}
		});

		fourButton.addButtonPressedListener(new IButtonPressedListener() {

			public void buttonPressed(ButtonPressedEvent arg0) {
				players=4;
				buildMainMenu();
			}
		});
		

	}

	public void buildPlayersMenu() {
		menu.removeAllWidgets();
		menu.addWidget(oneButton);
		menu.addWidget(twoButton);
		menu.addWidget(threeButton);
		menu.addWidget(fourButton);
		menu.pack();
		StaticLayout.center(menu, display);
	}

	public void buildMainMenu() {
		menu.removeAllWidgets();
		menu.addWidget(playButton);
		menu.addWidget(playersButton);
		menu.addWidget(quitButton);
		menu.pack();
		StaticLayout.center(menu, display);
	}
	
	public void buildPauseMenu(){
		menu.removeAllWidgets();
		menu.addWidget(continueButton);
		menu.addWidget(endButton);
		menu.pack();
		StaticLayout.center(menu, display);
	}

	public final void setActive(final boolean active) {
		if (active == false) {
			MouseInput.get().setCursorVisible(false);
			handler.setEnabled(false);
			return;
		}
		finish=false;
		MouseInput.get().setCursorVisible(true);

		Texture defTex = TextureState.getDefaultTexture().createSimpleClone();
		defTex.setScale(new Vector3f(1, 1, 1));
		defaultTextureState = DisplaySystem.getDisplaySystem().getRenderer()
				.createTextureState();
		defaultTextureState.setTexture(defTex);

		GameTaskQueueManager.getManager().render(new Callable<Object>() {
			public Object call() throws Exception {
				for (int i = 0; i < RenderState.RS_MAX_STATE; i++) {
					defaultTextureState.apply();
				}
				return null;
			}
		});

		
	}

	public void draw() {
		display.display();
	}

	public boolean isFinish() {
		return finish;
	}

	public int getPlayers() {
		return players;
	}

	public boolean isChange() {
		return change;
	}
	
	
}
